package review;

import java.util.Arrays;
import java.util.Comparator;

import org.junit.Test;

public class MyTest {
	@Test
	public void ifTest() {
		// boolean表达式
		if (5>3) {}
		if (5<3) {}
		if (true) {}
		if (5 > 3 && 5 <=7) {}
		
		if (true) {
		} else {
		}
		
		int money = 1000;
		if (money <= 1000) {
			System.out.println(money * 0.9);
		}else if (money <= 2000) {
			System.out.println(money * 0.8);
		}else if (money <= 3000) {
			// 困难
		}else {
			
		}
	}

	@Test
	public void switchTest() {
		// switch(byte,short,char,int,enum,String)
		// 匹配几个固定的值（页面）
		String color = "yellow";
		switch (color) {
		case "red": System.out.println();break; 
		case "yellow": System.out.println("a");
		case "green": System.out.println("b"); 
		default: System.out.println("default");
		}
	}

	@Test
	public void array() {
		int[] arr = {1,42,123,63,123,24};
		// 走下标
		for(int i = 0 ; i < arr.length; i ++) {
			System.out.println(arr[i]);
		}
	
		// 走值(遍历效率高)
		for(int i : arr) {
			System.out.println(i);
		}
		
		// 数组拷贝
		int[] arr2 = arr.clone();
		
		int[] arr3 = Arrays.copyOf(arr, 10);
		
		int[] arr4 = new int[10];
		System.arraycopy(arr, 0, arr4, 0, arr.length);
		
		System.out.println(Arrays.toString(arr2));
		System.out.println(convertToString(arr3));
		System.out.println(convertToString(arr4));
		
		
		Arrays.sort(arr2);
		System.out.println(Arrays.toString(arr2));
		
		// 对于引用类型数组，可以定制比较方法
		Integer[] arr5 = {1,42,123,63,123,24};
		Arrays.sort(arr5, new Comparator<Integer>() {
			@Override
			public int compare(Integer o1, Integer o2) {
				return o2 - o1;
			}
		});
		System.out.println(Arrays.toString(arr5));
	}
	
	// "[1, 42, 123, 63, 123, 24]"
	private String convertToString(int[] arr) {
		StringBuilder value = new StringBuilder("[");
		for (int i : arr) {
			value.append(i).append(",");
		}
		// 去掉最后一个逗号
		value.deleteCharAt(value.length()-1).append("]");
		return value.toString();
	}

}








